<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/6 16:37}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\model;
use think\Db;
use think\Model;
use app\api\model\UserHistory as UserHistoryModel;
use app\api\model\GoodsItem as GoodsItemModel;
use app\api\model\Config as ConfigModel;

class UserAddress extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**
     * @param string $ids
     * @param $uid
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 删除地址
     */
    public function del($ids = '', $uid)
    {
        $ids = explode(',',$ids);

        foreach ($ids as $val)
        {
            $userAddreesId = Db::name('user')->where('id',$uid)->value('address_id');
            if ($userAddreesId == $val)
            {
                continue;
            }
            Db::name('user_address')->where('uid',$uid)->where('id',$val)->delete();
        }
        return true;
    }

    /**
     *获取地址信息
     */
    public function getAddress($id)
    {
        $res = Db::name('user_address')->where('id',$id)->find();
        if (!$res) return false;
        return $res;
    }

    /**
     * 地址列表
     */
    public function getaddresslist($uid)
    {
        $response = Db::name('user_address')->where('uid',$uid)->select();
        if ($response)
        {
            return $response;
        }else{
            return false;
        }
    }

    /**
     * 安装店地址
     */
    public function getshopaddress($id)
    {
        $response = Db::name('user_shop')->field(
            'id,shop_name,province,city,address,phone,shop_images,area'
        )->where('id',$id)->find();
        if (!$response) return false;
        $response['shop_images'] = explode(',',$response['shop_images']);
        return $response;
    }

    /***
     * 用户默认地址
     */
    public function Getuseraddress($uid)
    {
      $addressid = Db::name('user')->where('id',$uid)->value('address_id');
      $response = Db::name('user_address')->field('create_time',true)->where('id',$addressid)->find();
      if (!$response) return false;

      return $response;
    }
}